package com.sean.framework.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.provider.Contacts.People;
import android.view.KeyEvent;

import com.sean.framework.R;

public class AppDialogUtil {

	private static ProgressDialog progressDialog;

	public static ProgressDialog showLoading(Context context) {

		if (progressDialog == null) {
			progressDialog = new ProgressDialog(context);
			progressDialog.setTitle(R.string.app_title);
			progressDialog.setMessage(context.getString(R.string.str_loading));
			progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressDialog.setOnKeyListener(new OnKeyListener() {

				public boolean onKey(DialogInterface dialog, int keyCode,
						KeyEvent event) {
					if (keyCode == KeyEvent.KEYCODE_BACK
							&& event.getRepeatCount() == 0) { // 按下的如果是BACK，同时没有重复
						return true;
					}
					return false;
				}
			});
		}

		progressDialog.show();
		return progressDialog;
	}

	public static ProgressDialog showUploadLoading(Context context, int max) {

		if (progressDialog == null) {
			progressDialog = new ProgressDialog(context);
			progressDialog.setTitle(R.string.app_title);
			progressDialog.setMessage(context
					.getString(R.string.str_uploadding));
			progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			progressDialog.setOnKeyListener(new OnKeyListener() {

				@Override
				public boolean onKey(DialogInterface dialog, int keyCode,
						KeyEvent event) {
					if (keyCode == KeyEvent.KEYCODE_BACK
							&& event.getRepeatCount() == 0) { // 按下的如果是BACK，同时没有重复
						return true;
					}
					return false;
				}
			});
			// progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			progressDialog.setMax(max);
		}

		progressDialog.show();
		return progressDialog;
	}

	public static void closeLoading() {
		if (progressDialog != null) {
			progressDialog.hide();
			progressDialog.cancel();
			progressDialog = null;
		}
	}

	public static void showAlert(final Context context, String text) {
		AppDialogUtil.showAlert(context, text, false);
	}

	public static void showAlert(final Context context, String text,
			final boolean willFinish) {
		closeLoading();

		Dialog dialog = new AlertDialog.Builder(context)
				.setIcon(android.R.drawable.stat_notify_error)
				.setTitle(R.string.app_title)
				.setMessage(text)
				.setNeutralButton(R.string.str_ok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface d,
									int whichButton) {
								d.cancel();
								if (willFinish && context instanceof Activity) {
									((Activity) context).finish();
								}
							}
						}).create();

		dialog.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK
						&& event.getRepeatCount() == 0) { // 按下的如果是BACK，同时没有重复
					return true;
				}
				return false;
			}
		});

		dialog.show();
	}

	public static void showConnectionNotOpenAlert(final Activity activity,
			final boolean willFinish) {
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setIcon(android.R.drawable.stat_notify_error);
		builder.setTitle(activity.getString(R.string.app_title));
		builder.setMessage(R.string.str_connect_error);
		builder.setNeutralButton(R.string.str_setting,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface d, int whichButton) {
						d.cancel();
						activity.startActivityForResult(
								new Intent(
										android.provider.Settings.ACTION_WIRELESS_SETTINGS),
								0);
						if (willFinish) {
							activity.finish();
						}
					}
				});
		if (willFinish) {
			builder.setNegativeButton(R.string.str_close,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface d, int whichButton) {
							d.cancel();
							if (willFinish) {
								activity.finish();
							}
						}
					});
		} else {
			builder.setNegativeButton(R.string.str_ok,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface d, int whichButton) {
							d.cancel();
						}
					});
		}
		AlertDialog alert = builder.create();

		alert.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK
						&& event.getRepeatCount() == 0) { // 按下的如果是BACK，同时没有重复
					return true;
				}
				return false;
			}
		});

		alert.show();
	}
}
